﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class AbsentManager
    {
        public AbsentInfo GetAbsentById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryAbsent = from d in db.Absents
                                    where d.Id.Equals(id)
                                    select new AbsentInfo { Id = d.Id,EmployeeId=d.EmployeeId,Reason=d.Reason,Date=d.Date};

                return queryAbsent.First();
            }
        
        }


        public IList<AbsentInfo> ListAbsent()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryAbsent = from d in db.Absents join c in db.Employees on d.EmployeeId equals c.Id
                                  select new AbsentInfo { Id = d.Id, EmployeeId = d.EmployeeId, Reason = d.Reason, Date = d.Date ,EmployeeName=c.Fullname};

                return queryAbsent.ToList();
            }
        }
    

        public void CreateAbsent(Absent Absent)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Absents.InsertOnSubmit(Absent);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Absent"></param>
        public void UpdateAbsent(Absent Absent)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Absent = db.Absents.Single(d => d.Id.Equals(Absent.Id));

                _Absent.EmployeeId = Absent.EmployeeId;
                _Absent.Date = Absent.Date;
                _Absent.Reason = Absent.Reason;
                
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAbsent(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Absent = db.Absents.Single(d => d.Id.Equals(id));

                db.Absents.DeleteOnSubmit(_Absent);
                db.SubmitChanges();
            }
        }


   
    }
}
